// "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36 QIHU 360SE"
// "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
// "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
// 火狐
// "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0"
// Edg
// 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.41'
// Chrome
// 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'
// QQBrowser
// "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3879.400 QQBrowser/10.8.4552.400"
// 搜狗
// "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0"
// Uc
// "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.3964.2 Safari/537.36"

var BrowserMatch = {
  init: function () {
      this.browser = this.getBrowser().browser || "An Unknown Browser";
      this.version = this.getBrowser().version || "An Unknown Version";
      this.OS = this.getOS() || "An Unknown OS";
  },
  getOS: function () {
      if (navigator.platform.indexOf("Win") != -1) return "Windows";
      if (navigator.platform.indexOf("Mac") != -1) return "Mac";
      if (navigator.platform.indexOf("Linux") != -1) return "Linux";
      if (navigator.userAgent.indexOf("iPhone") != -1) return "iPhone/iPod";
  },
  isLowBrowser: function () {
      const browser = this.getBrowser();
      const browserVersion = {
          ie: 11,
          chrome: 70,
          safari: 10,
          firefox: 84,
      };
      return browserVersion[browser.browser] >= String(browser.version).slice(0, 2);
  },
  // 判断ie的版本
  IEVersion: function IEVersion() {
      var userAgent = navigator.userAgent; // 取得浏览器的userAgent字符串
      var isIE =
          userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; // 判断是否IE<11浏览器
      // var isEdge = userAgent.indexOf('Edge') > -1 && !isIE // 判断是否IE的Edge浏览器
      var isIE11 =
          userAgent.indexOf("Trident") > -1 && userAgent.indexOf("rv:11.0") > -1;
      if (isIE) {
          // var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
          // reIE.test(userAgent);
          var fIEVersion = Number(parseFloat(RegExp.$1));
          if (fIEVersion === 7) {
              return 7;
          } else if (fIEVersion === 8) {
              return 8;
          } else if (fIEVersion === 9) {
              return 9;
          } else if (fIEVersion === 10) {
              return 10;
          } else {
              return 6; // IE版本<=7test
          }
      } else if (isIE11) {
          return 11; // IE11
      } else {
          return false; // 不是ie浏览器
      }
  },
  getBrowser: function () {
      const isIE = this.IEVersion();
      if (isIE) {
          return {browser: "ie", version: isIE};
      }

      var rFirefox = /(firefox)\/([\w.]+)/;
      var rOpera = /(opera).+version\/([\w.]+)/;
      var rNewOpera = /(opr)\/(.+)/;
      var rChrome = /(chrome)\/([\w.]+)/;
      var rSafari = /version\/([\w.]+).*(safari)/;
      var ua = navigator.userAgent.toLowerCase();
      var matchBS, matchBS2;
      matchBS = rFirefox.exec(ua);
      if (
          matchBS != null &&
          !window.attachEvent &&
          !window.chrome &&
          !window.opera
      ) {
          return {browser: matchBS[1] || "", version: matchBS[2] || "0"};
      }
      matchBS = rOpera.exec(ua);
      if (matchBS != null && !window.attachEvent) {
          return {browser: matchBS[1] || "", version: matchBS[2] || "0"};
      }
      matchBS = rChrome.exec(ua);
      if (matchBS != null && !!window.chrome && !window.attachEvent) {
          matchBS2 = rNewOpera.exec(ua);
          if (matchBS2 == null) {
              return {browser: matchBS[1] || "", version: matchBS[2] || "0"};
          } else {
              return {browser: "Opera", version: matchBS2[2] || "0"};
          }
      }
      matchBS = rSafari.exec(ua);
      if (
          matchBS != null &&
          !window.attachEvent &&
          !window.chrome &&
          !window.opera
      ) {
          return {browser: matchBS[2] || "", version: matchBS[1] || "0"};
      }
  },
};
export default BrowserMatch;
